/*****************************************************************************
 * visual.c : Visualisation system
 *****************************************************************************
 * Copyright (C) 2002-2009 VLC authors and VideoLAN
 * $Id: b4c72201b8fffeb88d9c5849140cc374c533ac04 $
 *
 * Authors: Clément Stenac <zorglub@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <limits.h>
#include <malloc.h>
#include <string.h>

#include "visual.h"
#include "../format.h"
#include "../base.h"


/*****************************************************************************
 * Open: open the visualizer
 *****************************************************************************/
int init(filter_sys_t *p_sys)
{

    if( p_sys == NULL )
        return -1;

    p_sys->i_effect = 1;
    p_sys->effect   = NULL;

    visual_effect_t *p_effect;

    formate_Open(&p_sys->format);

    p_effect = malloc( sizeof( visual_effect_t ) );
    if( !p_effect ) {
        free(p_sys);
        return -1;
    }
    //TODO
    p_effect->i_nb_chans  = p_sys->i_nb_chans;
    p_effect->i_brand_80 = p_sys->i_brand_80;

    p_effect->p_data   = NULL;
    p_effect->pf_run   = NULL;

    p_effect->pf_run = effectv[0].run_cb;
    p_effect->pf_free = effectv[0].free_cb;

    p_sys->effect = p_effect;

    LOGE( "VLC_CODEC_S16N::::: %d  %p" ,VLC_CODEC_S16N,p_sys);
    if( !p_sys->effect )
    {
        LOGE( "no effects found" );
        goto error;
    }
    return 0;

error:
//    for( int i = 0; i < p_sys->i_effect; i++ )
    //free( p_sys->effect );
    free( p_sys->effect );
    free( p_sys );
    return -1;
}

block_t *DoWork( filter_sys_t *p_sys, block_t *p_in_buf, int *dst)
{

    p_in_buf = p_sys->format.pf_audio_filter(p_in_buf);

#define p_effect p_sys->effect
    if( p_effect->pf_run )
    {
        p_effect->pf_run( p_effect, p_in_buf, dst);
    }
#undef p_effect

    return p_in_buf;
}


/*****************************************************************************
 * Close: close the plugin
 *****************************************************************************/
void Close( filter_sys_t *p_sys)
{

#define p_effect (p_sys->effect)
    p_effect->pf_free( p_effect->p_data );
    free( p_effect );
#undef p_effect

    free( p_sys->effect );
    free( p_sys );
}
